﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.Video;

namespace GUI.Controls.Video
{
    public partial class BoxVideo : System.Web.UI.UserControl
    {
        public string Theme { get; set; }
        public string Title { get; set; }
        public int ZoneId { get; set; }
        public string ViewMoreUrl { get; set; }
        public string Videos { get; set; }

        private const string DefaultViewMoreUrl = "/video.htm";
        public int Top = 6;
        private const int ImageWidth = 180;
        private const int ImageHeight = 100;
        private const string Format = "<li rel=\"{5}\">{4}<p class=\"publishedDate\">{3}</p><a class=\"title\" href=\"{0}\" title=\"{2}\">{1}</a> <img src=\"http://kenh143.vcmedia.vn/skin/listvideo/video-view-icon.png\" class=\"icon-video\"></li>";
        private const string PublishedDateFormat = "HH:mm:ss - dd/MM/yyyy";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ZoneId >= 0)
            {
                var dt = VideoBO.GetNewestVideo(Top, ZoneId);
                Visible = DataBind(dt);
                if (Visible && ZoneId > 0 && string.IsNullOrEmpty(ViewMoreUrl))
                {
                    var zone = VideoBO.GetVideoZoneDetail(ZoneId);
                    if (zone != null && !string.IsNullOrEmpty(zone.Url))
                    {
                        ViewMoreUrl = zone.Url;
                    }
                    else
                    {
                        ViewMoreUrl = DefaultViewMoreUrl;
                    }
                }
                return;
            }
            Visible = false;
        }

        private bool DataBind(System.Data.DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildVideoAvatar(r, ImageWidth, ImageHeight);
                    sb.AppendFormat(Format,
                        r[SqlConstants.Video.Url],
                        r[SqlConstants.Video.Name],
                        r[SqlConstants.Video.EncodedName],
                        PresentationUtils.Object2DateTime(r[SqlConstants.Video.PublishDate].ToString()).ToString(PublishedDateFormat),
                        avatar,
                        r[SqlConstants.Video.Id]);
                }
                ltrData.Text = sb.ToString();
                Videos = PresentationUtils.ObjectToJson(dt);
                return true;
            }
            return false;
        }
    }
}